package com.xdl.backstage.base.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.xdl.backstage.base.bean.CompanyBean;
import com.xdl.backstage.base.bean.dto.CompanyDTO;
import com.xdl.backstage.base.dao.CompanyDao;
import com.xdl.backstage.base.dao.UserManagerDao;
import com.xdl.backstage.base.domain.Company;
import com.xdl.backstage.base.domain.UserManager;
import com.xdl.backstage.base.entity.CompanyEntity;
import com.xdl.backstage.base.entity.UserManagerEntity;
import com.xdl.backstage.base.service.iface.CompanyService;
import com.xdl.backstage.base.service.iface.UserManagerService;
import com.xdl.backstage.base.service.logic.CompanyServiceLogic;
import com.xdl.backstage.base.service.logic.UserServiceLogic;
import com.xdl.moudle.utils.util.EnumUtil.OpearteMode;

@Service("CompanyService")
public class CompanyServiceImpl implements CompanyService {

	@Autowired
	private CompanyDao companyDao;
	@Autowired
	private UserManagerService userManagerService;
	@Autowired
	private UserManagerDao userManagerDao;
	@Override
	public CompanyEntity getData(String id) {
		if (StringUtils.isNotBlank(id)) {
			Company data = companyDao.selectByPrimaryKey(id);
			if (data != null) {
				CompanyEntity entity = new CompanyEntity(data);
				UserManagerEntity createUserEntity = userManagerService.getData(data.getCreateUser());
				entity.setCreateUser(createUserEntity);
				UserManagerEntity updateUserEntity = userManagerService.getData(data.getUpdateUser());
				entity.setUpdateUser(updateUserEntity);
				return entity;
			}
		}
		return null;
	}

	@Override
	public List<CompanyEntity> queryDataByParams(CompanyDTO dto) {
		List<Company> listData = companyDao.queryDataByParams(dto);
		List<CompanyEntity> resultDatas = new ArrayList<>();
		if (listData != null) {
			for (Company company : listData) {
				CompanyEntity entity = new CompanyEntity(company);
				UserManager createUser = UserServiceLogic.getInstance(userManagerDao).getData(company.getCreateUser());
				UserManagerEntity createUserEntity = new UserManagerEntity(createUser);
				entity.setCreateUser(createUserEntity);
				UserManager updateUser = UserServiceLogic.getInstance(userManagerDao).getData(company.getUpdateUser());
				UserManagerEntity updateUserEntity = new UserManagerEntity(updateUser);
				entity.setUpdateUser(updateUserEntity);
				resultDatas.add(entity);
			}
		}
		return resultDatas;
	}

	@Override
	@Transactional
	public boolean save(CompanyBean[] arrCompany) {
		boolean isSuccess = false;
		if (arrCompany != null) {
			for (CompanyBean companyBean : arrCompany) {
				Company data = companyBean.beanToData();
				if (companyBean.getOpearteMode().getStatus() == OpearteMode.NEW.getStatus()) {
					isSuccess = companyDao.insertSelective(data) > 0;
				} else if (companyBean.getOpearteMode().getStatus() == OpearteMode.MODIFED.getStatus()) {
					isSuccess = companyDao.updateByPrimaryKeySelective(data) > 0;
				} else if (companyBean.getOpearteMode().getStatus() == OpearteMode.DELETED.getStatus()) {
					isSuccess = companyDao.deleteByPrimaryKey(data.getId()) > 0;
				}
			}
		}
		return isSuccess;
	}

	@Override
	public boolean delete(String id) {
		return companyDao.deleteByPrimaryKey(id) > 0;
	}

}
